package com.jcfk.eam.controller.main;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.eam.domain.query.main.EamWorkOrderQuery;
import com.jcfk.eam.domain.dto.main.EamWorkOrderDTO;
import com.jcfk.eam.domain.dto.main.EamWorkOrderRecordDTO;
import com.jcfk.eam.service.main.EamWorkOrderService;

import java.util.List;

/**
 * 作业工单
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-09-04
 */
@Api(tags = {"作业工单管理"})
@RestController
@RequestMapping("eamworkorder")
public class EamWorkOrderController {
    @Autowired
    private EamWorkOrderService eamWorkOrderService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamWorkOrderQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询作业工单")
    @PostMapping("/queryByPage")
    public RetPage<EamWorkOrderDTO> queryByPage(@RequestBody ReqPageParam<EamWorkOrderQuery> requestParam) {
        DataPage<EamWorkOrderDTO> page = new DataPage<EamWorkOrderDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamWorkOrderDTO.class);
        page = eamWorkOrderService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamWorkOrderQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取作业工单列表", notes = "获取作业工单列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamWorkOrderRecordDTO>> getDetails(@RequestBody EamWorkOrderQuery query) {

        List<EamWorkOrderRecordDTO> list = eamWorkOrderService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamWorkOrderQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取作业工单列表", notes = "获取作业工单列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamWorkOrderDTO>> getList(@RequestBody EamWorkOrderQuery query) {

        List<EamWorkOrderDTO> list = eamWorkOrderService.getList(query);

        return RetResult.ok(list);
    }


    /**
     *  作业工单
     * 根据id 查询详情
     */
    @ApiOperation("查询作业工单")
    @GetMapping("/get/{id}")
    public RetResult<EamWorkOrderDTO> get(@PathVariable("id") String id) {
            EamWorkOrderDTO item = eamWorkOrderService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理作业工单
     */
    @ApiOperation("保存作业工单")
    @PostMapping("/save")
    @RequestLogHeader(modular = "作业工单管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamWorkOrderDTO eamWorkOrder) {
        // 数据校验
        RetResult ret = eamWorkOrderService.isValid(eamWorkOrder);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamWorkOrder.getId())){
            // 新增
                eamWorkOrderService.insert(eamWorkOrder);
        } else{
            // 更新
                eamWorkOrderService.update(eamWorkOrder);
        }
        return RetResult.ok();

    }


    /**
     *  批量删除作业工单
     */
    @ApiOperation("批量删除作业工单")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "作业工单管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamWorkOrderDTO eamWorkOrder) {
        return RetResult.ok(eamWorkOrderService.removeBigBatch(eamWorkOrder.getIds()));
    }


    /**
     *  删除作业工单
     */
    @ApiOperation("删除作业工单")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "作业工单管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamWorkOrderService.deleteItem(id);
        return RetResult.ok();
    }
}
